package org.shejko.util.splitunite;

import java.util.Arrays;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * @author dmytros
 *
 */
public class DirectSplitUnite_utest
{
    SplitUniteHelper sut_;

    @Before
    public void setUp()
    {
        sut_ = new SplitUniteHelper(new DirectSplitUnite(';', '!'));
    }

    void check(String str, String... elems)
    {
        List<String> lst = Arrays.asList(elems);
        Assert.assertEquals("unite", str, sut_.unite(lst));
        Assert.assertEquals("split", lst, sut_.split(str));
    }

    @Test
    public void testSimpleTwo()
    {
        check("abc;def", "abc", "def");
    }

    @Test
    public void testSimpleOne()
    {
        check("abc", "abc");
    }

    @Test
    public void testEscapeSep()
    {
        check("ab;!cd", "ab;cd");
    }

    @Test
    public void testEscapeEsc()
    {
        check("ab;!!!cd", "ab;!cd");
    }

    @Test
    public void testEscapeEscAtHead()
    {
        check(";!abcd", ";abcd");
        check(";!!!!!abcd", ";!!abcd");
    }

    @Test
    public void testEscapeEscAtTail()
    {
        check("abcd;!", "abcd;");
        check("abcd;!!!!!", "abcd;!!");
        check("abcd!!!", "abcd!!!");
    }

    @Test
    public void testNoEscapeEsc()
    {
        check("ab!!!cd", "ab!!!cd");
    }

    @Test
    public void testEmptyList()
    {
        check("");
    }

    @Test
    public void testListWithOneEmpty()
    {
        check(";", "");
    }

    @Test
    public void testListWithAllEmpties()
    {
        check(";;;", "", "", "");
    }

    @Test
    public void testListWithOneEmptyAtHead()
    {
        check(";a", "", "a");
    }

    @Test
    public void testListWithOneEmptyAtTail()
    {
        check("b;", "b", "");
    }

    @Test
    public void testListWithOneEmptyInside()
    {
        check("ab;;cde", "ab", "", "cde");
    }

    @Test
    public void testException()
    {
        try
        {
            new DirectSplitUnite('?', '?');
            Assert.fail("IllegalArgumentException is expected");
        }
        catch (IllegalArgumentException exc)
        {
            Assert.assertEquals("sep == esc == \\u003f (?)", exc.getMessage());
        }
    }
}
